//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.Activities;

namespace TridentBasicActivities.Activities
{
    /// <summary>
    /// This class is used to validate the Replicator Conatainer activity.
    /// This will be used to check if the child of the replicatorContainer is ReplicatorActivity.
    /// Also, this will be used to check if the activity containd only one Activity.
    /// </summary>
    public class ValidateReplicator : CompositeActivityValidator
    {
        public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
        {
            if (null == manager)
            {
                throw new ArgumentNullException("manager");
            }

            if (null == obj)
            {
                throw new ArgumentNullException("obj");
            }

            Replicator pif = obj as Replicator;

            if (null == pif)
            {
                throw new ArgumentException("This validator can only be used by the ReplicatorActivity.", "obj");
            }

            ValidationErrorCollection errors = base.Validate(manager, obj);

            // Now actually validate the activity... 
            if (pif.Activities.Count > 1)
            {
                // If the activity contains more than one child activity.
                ValidationError err = 
                    new ValidationError("Only one activity is allowed in the ReplicatorActivity.", 100, false);
                err.UserData.Add("MORE_THAN_ONE", "More than one Activitybranches");
                errors.Add(err);
            }
            else if (pif.Activities.Count == 1)
            {
                // if the child is not ReplicatorActivity.
                if (!pif.Activities[0].GetType().Equals(typeof(ReplicatorActivity)))
                {
                    ValidationError err = 
                        new ValidationError("Only out of box ReplicatorActivity is allowed inside custom ReplicatorActivity.", 100, false);
                    err.UserData.Add("ONLY_REPLICATORACTIVITY", "Only replicator activity allowed");
                    errors.Add(err);
                }
            }
            else 
            {
                // If the activity does not contain any child activity.
                ValidationError err = 
                    new ValidationError("ReplicatorActivity must contain only one activity.", 100, false);
                err.UserData.Add("NO_ACTIVITY", "No Activity present.");
                errors.Add(err);
            }

            return errors;
        }
    }
}
